import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

public class Test1 {
    // 删除有序数组中的重复项
    public static int removeDuplicates2(int[] nums) {
        int fast = 1;
        int slow = 1;

        while (fast < nums.length) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }

    public static int removeDuplicates1(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length - 1 - j;) {
            if (nums[i] == nums[i + 1]) {
                j++;
                for (int k = i; k < nums.length - j; k++) {
                    nums[k] = nums[k + 1];
                }
            } else {
                i++;
            }
        }
        return nums.length - j;
    }

    public static void main(String[] args) {
        int[] nums = {0,0,1,1,1,2,2,3,3,4};
        removeDuplicates2(nums);
    }

    //合并两个有序数组
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0, p2 = 0;
        int[] sorted = new int[m + n];
        int cur;
        while (p1 < m || p2 < n) {
            if (p1 == m) {
                cur = nums2[p2++];
            } else if (p2 == n) {
                cur = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums1[p1++];
            } else {
                cur = nums2[p2++];
            }
            sorted[p1 + p2 - 1] = cur;
        }
        for (int i = 0; i != m + n; ++i) {
            nums1[i] = sorted[i];
        }
    }

    public static void main2(String[] args) {
        int[] nums1 = new int[6];
        nums1[0] = 1;
        nums1[1] = 2;
        nums1[2] = 3;
        int[] nums2 = new int[]{2,5,6};
        merge(nums1,3,nums2,3);
        System.out.println(Arrays.toString(nums1));
        System.out.println(nums1.length);
    }

    //杨辉三角
    public List<List<Integer>> generate (int numRows) {
        List<List<Integer>> ret = new ArrayList<>();

        List<Integer> row = new ArrayList<>();
        row.add(1);
        ret.add(row);

        for (int i = 1; i < numRows; i++) {
            List<Integer> curRow = new ArrayList<>();
            curRow.add(1);
            for (int j = 1; j < i; j++) {
                curRow.add((ret.get(i - 1)).get(j)  + (ret.get(i - 1)).get(j - 1));
            }
            curRow.add(1);
            ret.add(curRow);
        }
        return ret;
    }

    public static void main1(String[] args) {

        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(1,2);
        System.out.println(arrayList);

        System.out.println("================");
        arrayList.remove(1);
        System.out.println(arrayList);


        System.out.println("================");
        arrayList.remove(new Integer(1));
        System.out.println(arrayList);

        System.out.println("================");
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);

        ArrayList<Integer> arrayList1 = new ArrayList<>(list);
        arrayList1.add(3);
        System.out.println(arrayList1);

        System.out.println("================");
        System.out.println(arrayList1.get(0));
        arrayList1.set(0, 5);
        System.out.println(arrayList1);

        System.out.println("================");

        System.out.println(arrayList1.contains(1));

        System.out.println("================");

        List<Integer> sub = arrayList1.subList(0,2);//左闭右开
        System.out.println(sub);

        System.out.println("================");

        sub.set(0,99);
        System.out.println(sub);
        System.out.println(arrayList1);

        System.out.println("=================");
        for (int i = 0; i < arrayList1.size(); i++) {
            System.out.print(arrayList1.get(i) + " ");
        }
        System.out.println();

        System.out.println("=================");
        for (Integer x: arrayList1) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("=================");
        //迭代器
        ListIterator<Integer> it = arrayList1.listIterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
    }
}
